<template>
  <div>
    <div class="form-wrap">
      <input
        v-model="description"
        class="input-block-plain"
        type="text"
        :placeholder="account.description"
        placeholder-class="placeholder-part"
        maxlength="50"
      >
      <span class="count">{{countText}}</span>
      <button class="btn-round-blue" @click="handleUpdateShopInfo">完成</button>
    </div>
  </div>
</template>

<script>
import {} from '@/utils'
import { UPDATE_SHOP_BASE_INFO } from '@/api'

export default {
  components: {},
  props: {},
  data () {
    return { description: '' }
  },
  computed: {
    account () {
      return this.$store.state.account
    },
    countText () {
      if (this.description) {
        return this.description.length + '/50'
      } else {
        return this.account.description.length + '/50'
      }
    }
  },
  watch: {},
  created () {},
  mounted () {},
  onShow () {},
  methods: {
    handleUpdateShopInfo () {
      if (!this.description) {
        this.$toast('请输入店铺简介')
      } else {
        this.updateShopInfo()
      }
    },
    updateShopInfo () {
      const vm = this

      let params = {
        shopId: vm.account.shopId,
        description: vm.description
      }

      UPDATE_SHOP_BASE_INFO(params)
        .then(r => {
          if (r.data.code === '0' && r.data.data) {
            vm.$toast('操作成功')
            vm.$store.commit('SET_ACCOUNT_INFO', {
              description: vm.description
            })
            vm.description = ''
            vm.$router.go(-1)
          } else {
            vm.$toast(r.data.message)
          }
        })
        .catch(e => {
          console.error(e)
        })
    }
  }
}
</script>

<style lang="scss" scoped>
@import 'static/styles/color.scss';

.form-wrap {
  position: relative;
  > .input-block-plain {
    width: calc(100% - 60px);
    padding: 5px 50px 5px 10px;
  }
  > .count {
    position: absolute;
    top: 13px;
    right: 20px;
    font-size: 12px;
    line-height: 12px;
    color: $gray;
  }
}
</style>
